package eu.nikoladichev.test.mdb;

import java.util.Date;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import javax.naming.NamingException;

import org.jboss.logging.Logger;

import eu.nikoladichev.test.utils.JndiIntialContextUtil;

/**
 * Message-Driven Bean implementation class for: MishoMDB
 *
 */
@MessageDriven(
		activationConfig = { @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
							 @ActivationConfigProperty(propertyName = "destination", propertyValue = "queue/Misho") ,
							 @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge")
						   }
			  )
public class MishoMDB implements MessageListener {

	Logger logger = Logger.getLogger(MishoMDB.class.getSimpleName());
	
	public void onMessage(Message message) {
    	try {
			processMessage(message);
		} catch (JMSException e) {
			logger.warn("JMSException" + e.getMessage());
		} catch (NamingException e) {
			logger.warn("NamingException" + e.getMessage());
		}
    }
    
    public void processMessage(Message message) throws JMSException, NamingException {
    	TextMessage msg = (TextMessage) message;
    	logger.info("Misho message body: " + msg.getText());
    	//msg.setStringProperty("queue", MishoMDB.class.getSimpleName());  // MESSAGE IS READONLY
    	//msg.setLongProperty("arrivedInQueue", Calendar.getInstance().getTimeInMillis());
    	logger.info("Arrived in: " + new Date(msg.getLongProperty("arrivedInQueue")) + ", queue is" + msg.getStringProperty("queue"));
    	//JndiIntialContextUtil.sendMessage(msg, "queue/KokoD");
    }
}
